<?php
namespace App\Http\Controllers;

use App\Order;
use App\Product;
use App\Rad_users;
use App\User;

use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;


use App\Http\Controllers\UsersController;
use App\Http\Controllers\UserWebController;


class OrderController extends Controller
{

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     * 获取订单列表
     */
    public function getOrderList(Request $request)
    {
        $page_size = 10;
        $page_no = 0;

        if (empty($request->input('token'))){
            $return = array(
                'code' => -1,
                'msg' => "非法参数",
                'data' => array()
            );
            return response()->json($return);
        }else{
            $token = $request->input('token');
            $user_id = UsersController::getIdByWebToken($token);

            if(!$user_id)
            {
                $return = array(
                    'code' => -1,
                    'msg' => "非法参数",
                    'data' => array()
                );
            }
        }
        if($request->has('page_no'))
        {
            $page_no = $request->input('page_no')-1;
            if( $page_no < 0) {
                $page_no = 0;
            }
        }

        if($request->has('page_size'))
        {
            $page_size = $request->input('page_size');
            if( $page_size <= 0) {
                $page_size = 10;
            }
        }

        $where = ['user_id'=>$user_id,'status'=>1];



        //   订单总数
        $count = Order::where($where)->count();


        //  订单分页页数
        $page_total = ceil( $count / $page_size );

        $orders = Order::where($where)->offset($page_no*$page_size)->limit($page_size)
            ->get(['id','order_no','date','connect_num','price','created_at','to_user'])->toArray();


        foreach($orders as &$value)
        {
            $user = Rad_users::where('user_id',$value['to_user'])->first();
            unset($value['to_user']);
            $value['name'] = $user->username;
        }

        $return = array(
            'code' => 0,
            'msg' => 'OK',
            'data' => array(
                'count'=>$count,
                'page_no' => $page_total,
                'orders' => $orders
            )
        );

        return response()->json($return);


    }

    /**
     * @param Request $request
     *
     */
    public function createAliPayOrder(Request $request)
    {
        $time = date('Y-m-d H:i:s',time());
        $product_id = '';

        if($request->has('id'))
        {
            $product_id = $request->input('id');
            $ret = Product::find($product_id);

            if(!$ret)
            {
                $return = array(
                    'code' => -1,
                    'msg' => '非法的产品ID',
                    'data' => array()
                );
            }
        }else{
            $return = array(
                'code' => -1,
                'msg' => '缺少产品ID',
                'data' => array()
            );
            return response()->json($return);
        }

        $token ='';
        if (empty($request->input('token'))){
            $return = array(
                'code' => -1,
                'msg' => "缺少用户token",
                'data' => array()
            );
            return response()->json($return);
        }else{
            $token = $request->input('token');
            $user_id = UsersController::getIdByWebToken($token);
            if(!$user_id)
            {
                $return = array(
                    'code' => -1,
                    'msg' => "非法的token",
                    'data' => array()
                );
            }
        }

        $where = [];
        $order_id = self::createUuid();
        $where['user_id'] = $user_id;
        $where['product_id'] = $product_id;
        $where['price'] = $ret->price_discount;
        $where['order_id'] = $order_id;
        $where['status'] = 0;
        $where['pay_channel'] = 1;
        $where['created_at'] = $time;
        $where['updated_at'] = $time;



        $id = Order::insertGetId($where);

        if($id)
        {
            $return = array(
                'code' => 0,
                'msg' => '',
                'data' =>$where
            );
            return response()->json($return);
        }

    }





    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|string|\Symfony\Component\HttpFoundation\Response
     *
     * 订单号
     */
    public function getOrderNo(Request $request)
    {
        if (empty($request->input('tissue')) || !$request->has('tissue') || $request->input('tissue') != md5('mibokeji'))
        {
            $return = array(
                'code' => -1,
                'msg' => '缺少产品ID',
                'data' => array()
            );
            return response()->json($return);
        }

        $order_no = self::createUuid();


        $return = array(
            'code' => 0,
            'msg' => 'OK',
            'order_no' =>$order_no
        );


        return response()->json($return);

    }



    /**
     * @param string $prefix
     * @return string
     * 生成唯一的订单号
     */


    //可以指定前缀   生成用户ID
    static function createUuid($prefix = "")
    {
        $str = md5(uniqid(mt_rand(), true));
        $uuid  = substr($str,0,8) . '-';
        $uuid .= substr($str,8,4) . '-';
        $uuid .= substr($str,12,4) . '-';
        $uuid .= substr($str,16,4) . '-';
        $uuid .= substr($str,20,12);
        return $prefix . $uuid;
    }


}